<?php
// +----------------------------------------------------------------------
// |[ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Date: 2016-10-24 下午6:06
//+----------------------------------------------------------------------
namespace Home\Lib\Wethird;
require_once('config.php');
require_once('crypt/wxBizMsgCrypt.php');

class Wxthird
{

    protected $appid;
    protected $appsecret;
    private $_token_file = '../base_token.php';
    
    public function __construct($config)
    {

        $this->appid = trim(AppID);
        $this->appsecret = trim(AppSecret);

        $this->_token_file = dirname(__FILE__).'/'.$this->_token_file;
        $this->_getBaseToken();

    }

    private function _getBaseToken (){
        if(!file_exists($this->_token_file)){
            $this->_updateBaseToken();
        } else {
            $data = json_decode(trim(file_get_contents($this->_token_file)),true);
            if ($data['expire_time'] < time()) { // 过期了，则更新
                $this->_updateBaseToken();
            } else {
                $this->access_token = $data['access_token'];
            }
        }
    }
    private function _updateBaseToken(){
        $rst = json_decode(file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appid .'&secret='.$this->appsecret), true);
        if (isset($rst['access_token'])) {
            $this->access_token = $rst['access_token'];
            // -180 三分钟空余时间
            file_put_contents($this->_token_file,json_encode(array('access_token'=>$rst['access_token'], 'expire_time'=>time() + $rst['expires_in'] - 180)));
        } else {
            return false;
        }
    }


    //获取用户信息
    public function user_info($openid,$access_token){
        $url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
        $res = $this->http_request($url);
        return json_decode($res, true);
    }

    //使用refesh_token获取access_token
    public function oauth2_refresh_token($refresh_token){
        $url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$this->appid.'&grant_type=refresh_token&refresh_token='.$refresh_token;
        $res = $this->http_request($url);
        return json_decode($res, true);
    }

    //获取access_token
    public function oauth2_access_token($code)
    {
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid."&secret=".$this->appsecret."&code=".$code."&grant_type=authorization_code";
        $res = $this->http_request($url);
        return json_decode($res, true);
    }

    //公众号服务器配置验证
    public function checkSignature($token){
        $timestamp = $_GET['timestamp'];
        $nonce = $_GET['nonce'];
        $signature = $_GET['signature'];
        $array = array($timestamp,$nonce,$token);
        sort($array);
        $str = implode($array);
        $sign = sha1($str);
        if($sign == $signature){
            return true;
        }else{
            return false;
        }
    }
    //HTTP请求（支持HTTP/HTTPS，支持GET/POST）
    protected function http_request($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        $output = curl_exec($curl);
        curl_close($curl);
        // var_dump($output);
        return $output;
    }
}



?>
